package com.pristine.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pristine.dao.ICustomerDao;
import com.pristine.domain.CustomerEntity;
import com.pristine.service.wsdl.CustomerVO;
import com.pristine.service.wsdl.ICustomerService;
import com.pristine.util.EntityToVOConverter;
import com.pristine.util.VOToEntityConverter;

@Service("customerService")
@Transactional
public class CustomerServiceImpl implements ICustomerService{

	@Autowired
	ICustomerDao customerDao;

	@Override
	public CustomerVO getCustomerById(Integer id) {
		CustomerEntity entity= customerDao.find(id);
		CustomerVO customerVO = EntityToVOConverter.convert(entity);
		return customerVO;
	}

	@Override
	public Integer updateCustomer(CustomerVO customerVO) {
		CustomerEntity entity = VOToEntityConverter.convert(customerVO);
		customerDao.update(entity);;
		return entity.getId();
	}

	@Override
	public boolean deleteCustomer(Integer id) {
		try {
			CustomerEntity entity = customerDao.find(id);
			customerDao.remove(entity);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	public Integer createCustomer(CustomerVO customerVO) {
		CustomerEntity entity = VOToEntityConverter.convert(customerVO);
		customerDao.add(entity);
		return entity.getId();
	}

	@Override
	public boolean deletePhone(Integer id) {
		try {
			customerDao.deletePhone(id);
			return true;
		} catch (Exception e) {
			return false;
		}
		
	}

	@Override
	public List<CustomerVO> getAllCustomer() {
		List<CustomerEntity> list = customerDao.list();
		List<CustomerVO> voList=  new ArrayList<CustomerVO>();
		if(CollectionUtils.isNotEmpty(list)){
			for(CustomerEntity entity:list){
				voList.add(EntityToVOConverter.convert(entity));
			}
		}
		return voList;
	}
	
	
}
